package com.example.apartment.controller;

import com.example.apartment.common.ApiRestResponse;
import com.example.apartment.common.RespExceptionEnum;
import com.example.apartment.domain.Admin;
import com.example.apartment.domain.Room;
import com.example.apartment.service.RoomService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 宿舍控制类
 *
 * @author wuzexin
 * @date 2021/11/2 16:22
 */
@RestController
@RequestMapping("/admin/room")
public class RoomController {

    @Resource
    private RoomService roomService;

    /*
        新增宿舍
     */
    @PostMapping("/create")
    public ApiRestResponse create(Integer roomNum, Integer personCount, Integer price, String buildingName, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 新增宿舍
        Room room = roomService.create(admin, roomNum, personCount, price, buildingName);

        // 返回宿舍信息
        return ApiRestResponse.success(room);
    }

    /*
        修改宿舍信息
     */
    @PostMapping("/update")
    public ApiRestResponse update(Integer id, Integer roomNum, Integer personCount, Integer price, Integer buildingId, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 修改宿舍信息
        Room room = roomService.update(admin, id, roomNum, personCount, price, buildingId);

        // 返回宿舍信息
        return ApiRestResponse.success(room);
    }

    /*
        删除宿舍
     */
    @PostMapping("/delete")
    public ApiRestResponse delete(Integer id, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 删除宿舍
        roomService.delete(admin, id);

        // 返回信息
        return ApiRestResponse.success();
    }

    /*
        查询宿舍信息
     */
    @PostMapping("/select")
    public ApiRestResponse select(HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 查询宿舍信息
        List<Room> list = roomService.select(admin);

        // 返回宿舍信息
        return ApiRestResponse.success(list);
    }

    /*
        根据buildingName查询所有宿舍房间
     */
    @PostMapping("/selectByBuildingName")
    public ApiRestResponse selectByBuildingName(String buildingName, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 查询宿舍信息
        List<Room> list = roomService.selectByBuildingName(admin, buildingName);

        // 返回宿舍信息
        return ApiRestResponse.success(list);
    }

    /*
        根据id查询房间信息
     */
    @PostMapping("/selectById")
    public ApiRestResponse selectById(Integer id, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 查询宿舍信息
        Room room = roomService.selectById(admin, id);

        // 返回宿舍信息
        return ApiRestResponse.success(room);
    }

    /*
        根据buildingName,查询某楼空宿舍数量
     */
    @PostMapping("/selectEmptyRoomCount")
    public ApiRestResponse selectEmptyRoomCount(String buildingName, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        int count = roomService.selectEmptyRoomCount(admin, buildingName);

        return ApiRestResponse.success(count);
    }
}
